package javaobjectsandencapsulation.no6;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * 测试对Map应用泛型
 */
public class Test7 {
    public static void main(String[] args) {
        //1.创建多个狗狗对象
        Dog dog = new Dog("欧欧", "雪瑞纳");
        Dog dog1 = new Dog("亚亚", "拉布拉多");
        Dog dog2 = new Dog("美美", "哈士奇");
        Dog dog3 = new Dog("菲菲", "泰迪");
        //2.创建ArrayList集合对象并把多个狗狗对象放入其中
        Map<String, Dog> dogs = new HashMap<String, Dog>();    //标记元素类型
        dogs.put(dog.getName(), dog);
        dogs.put(dog1.getName(), dog1);
        dogs.put(dog2.getName(), dog2);
        dogs.put(dog3.getName(), dog3);
        //通过迭代器一次输出集合中所有狗狗的信息
        System.out.println("使用Iterator遍历，所有狗狗的昵称和品种分别是：");
        Set<String> keys = dogs.keySet(); //取出所有Key的集合
        Iterator<String> it = keys.iterator();    //获取Iterator对象
        while (it.hasNext()) {
            String key = it.next();       //取出key，无需类型强制转换
            Dog dogx = dogs.get(key);
            System.out.println(key + "\t" + dogx.getType());
        }
        //使用foreach语句输出集合中所有狗狗信息
        System.out.println("******************************************");
        for (String key:keys) {
            Dog dogx=dogs.get(key);
            System.out.println(key+"\t"+dogx.getType());
        }
    }
}
